﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Microsoft.Xna.Framework;

namespace EvoSim.Simulation
{
	public class World
	{
		public int Width = 300;
		public int Height = 300;

		List<Creature> creatureList = new List<Creature>();
		List<Entity> entityList = new List<Entity>();
		public ReadOnlyCollection<Creature> CreatureList
		{
			get;
			protected set;
		}
		public ReadOnlyCollection<Entity> EntityList
		{
			get;
			protected set;
		}

		public bool WorldHasEdges = true;

		public World()
		{
			CreatureList = creatureList.AsReadOnly();
			EntityList = entityList.AsReadOnly();
		}

		public void AddEntity(Entity entity)
		{
			entityList.Add(entity);
			entity.World = this;

			if (entity.GetType().IsSubclassOf(typeof(Creature)) || entity.GetType().IsEquivalentTo(typeof(Creature)))
				creatureList.Add((Creature)entity);
		}
		public void RemoveEntity(Entity entity)
		{
			entityList.Remove(entity);

			if (entity.GetType().IsInstanceOfType(typeof(Creature)))
				creatureList.Remove((Creature)entity);
		}

		public void RemoveAllEntities()
		{
			entityList.Clear();
			creatureList.Clear();
		}

		public virtual void Update(GameTime gameTime)
		{
			foreach (Entity entity in new List<Entity>(entityList))
				entity.Update(gameTime);

			foreach (Entity entity in new List<Entity>(entityList))
				if (entity.IsDead)
					RemoveEntity(entity);
		}
	}
}
